<template>
  <div class="big">
    <h1 style="text-align: center" class="bt">登录</h1>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="rules.username"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="rules.password"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          color="#ff5100">提交</van-button
        >
      </div>
    </van-form>
    <p class="reg" @click="jump">还没有账号，点击跳转到注册</p>
  </div>
</template>

<script>
import Vue from "vue";
import { Form, Field, Button, Toast } from "vant";
import { login } from "@/request/request";
import { setCookie } from "@/util/cookie";
Vue.use(Form);
Vue.use(Field);
Vue.use(Button);
Vue.use(Toast);

export default {
  data() {
    return {
      rules: {
        username: [{ required: true, message: "请填写用户名" }],
        password: [{ required: true, message: "请填写密码" }],
      },
      username: "",
      password: "",
    };
  },
  methods: {
    onSubmit(values) {
      login(values).then((res) => {
        if (res.error_code === 200) {
          setCookie("username", values.username, "7200");
          Toast({
            message: "登录成功！",
            onClose: () => {
              this.$router.push('/movie');
            },
          });
        } else {
          Toast.fail(res.msg);
        }
      });
    },
    jump(){
      this.$router.push('/register')
    }
  },
};
</script>

<style lang="scss" scoped>
.big{
  padding: 50px;
  .bt{
    margin-bottom: 10px;
  }
}
.reg{
  text-align: center;

}
</style>
